type ship :
    record
	startx : int
	starty : int
	endx : int
	endy : int
	mode : int
    end record

% Globals
var userx, usery, mousex, mousey : int
var blank : string
var comploc : array 1 .. 10, 1 .. 10 of int
var userloc : array 1 .. 10, 1 .. 10 of int
var compships : array 1 .. 5 of ship
var userships : array 1 .. 5 of ship
var gameover := 0
var shipkilluser : int := 0
var shipkillcomp : int := 0


% Window Definitions
var GridWin := Window.Open ("position:top;left,graphics:200;200,nobuttonbar,title:Your Grid")
var EnemWin := Window.Open ("position:top;right,graphics:200;200,nobuttonbar,title:Enemy Grid")
var OutWin := Window.Open ("nobuttonbar,position:bottom;left,title:Battleship Log")

% A few basic functions used throughout the program are included here
include "putout.t"
include "coloursq.t"
include "zeroar.t"

% Debug Functions (could be used as cheats)
include "putar.t"
include "putshiplocs.t"

% Includes + Globals for AI firing logic
var isahit : int := 0
var hitx : int := 0
var hity, pwn : int := 0
include "compcfire.t"
include "compfire.t"
include "compnoobfire.t"
include "comprfire.t"
% AI placement logic
include "compplace.t"

% Some UI functions
include "drawgrid.t"
include "getint.t"
include "mousetogrid.t"
include "getsquare.t"
include "refreshgrid.t"

include "shipgone.t"

% User ship firing/placement
include "userfire.t"
include "userplace.t"

p_drawgrid (GridWin)
p_drawgrid (EnemWin)

zeroar
Window.Select (OutWin)
put "Enter AI skill level:\n 0 : Noob\n 1 : Easy\n 2 : Hard\n 3 : Next to impossible\n"
var aiskill := getint

putout ("You Place")
Window.Select (GridWin)
userplace (5, 1)
userplace (4, 2)
userplace (3, 3)
userplace (3, 4)
userplace (2, 5)
putout ("Ok. Computer will place")
Window.Select (EnemWin)
compplace (5, 1)
compplace (4, 2)
compplace (3, 3)
compplace (3, 4)
compplace (2, 5)
Window.Select (OutWin)
fixgridup (pwn)
loop
    putout ("User's turn, Please select a square to fire at")
    userfire
    if aiskill = 1 then
	compfire
    elsif aiskill = 3 then
	compcfire
    elsif aiskill = 0 then
	compcrapfire
    else
	comprfire
    end if
    isshipgoner
    if shipkilluser = 5 then
	putout ("You won the game!")
	exit
    elsif shipkillcomp = 5 then
	putout ("Comp won the game!")
	exit
    end if
    exit when gameover = 1
end loop
Window.Close (EnemWin)
Window.Close (GridWin)
Window.SetActive (OutWin)
put "Press enter to exit"
get blank : *
Window.Close (OutWin)
